
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;
import com.huizhan.web.view.*;

/**
 * 证件api
 */
 
@Api(tags = "Document",description = "证件")
@RestController
public class DocumentController {
    
    @Autowired
    private DocumentTypeDao daoType;

    @Autowired
    private DocumentDao dao;
   
    /**
     * 获取证件集合
     * @return api结果
     */
    @ApiOperation(value = "获取证件集合")
    @RequestMapping(value =  "/api/Document/",method = {RequestMethod.GET})
    public ApiResult<List<Document>> Get(){
         ApiResult<List<Document>> result=new ApiResult<List<Document>>();
        try 
        {
            List<Document> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取证件信息
     * @param id 证件ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取证件信息")
    @RequestMapping(value =  "/api/Document/{id}",method = {RequestMethod.GET})
    public ApiResult<Document> Get(@PathVariable Long id){
        ApiResult<Document> result=new ApiResult<Document>();
        try
        {
            Document model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 查询赠票集合
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询赠票集合")
    @RequestMapping(value =  "/api/Document/Search",method = {RequestMethod.POST})
    public ApiResult<List<Document>> Search(@RequestBody PageDocument model){
         ApiResult<List<Document>> result=new ApiResult<List<Document>>();
         
        try
        {    
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"document_Id") ;            
            Page<Document> page = dao.findByWhere(model.getName(),model.getCode(),model.getProjectId(),model.getDocumentTypeId(),model.getIdNumber(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        
        return result;
    }

    
    /**
     * 批量新增证件信息
     * @param id 证件类型ID
     * @param list 证件信息
     * @return api结果
     */
    @ApiOperation(value = "批量新增证件信息")
    @RequestMapping(value =  "/api/Document/AddList/{id}",method = {RequestMethod.PUT})
    public ApiResult<Document> AddList(@PathVariable Long id,@RequestBody List<Document> list){
        ApiResult<Document> result=new ApiResult<Document>();
        try
        {
          Optional<DocumentType> type = daoType.findById(id);

          if(type.isPresent()){
                List<Document> ds=new ArrayList<Document>();
                List<Document> list2=dao.findByProjectId(type.get().getProjectId());

                for (Document document : list) {
                    
                    document.setCreateDate(new Date());
                    document.setDocumentTypeId(id);
                    document.setTypeName(type.get().getName());
                    document.setProjectId(type.get().getProjectId()); 

                    list2.forEach(a->{
                        if(a.getCode().equals(document.getCode())){
                           ds.add(document);
                           return;
                        }
                    });
                }

                list.removeAll(ds);

                if(list.size()>0){
                    dao.saveAll(list);
                }

                result.setTotal(Long.parseLong(String.valueOf(list.size())));
                result.setMessage("导入成功:"+list.size()+"条,未导入相同号码证件:"+ds.size());
            }else{
                result.setHasError(true);
                result.setMessage("参数错误");
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }


    /**
     * 修改证件信息
     * @param id 证件ID
     * @param model 证件信息
     * @return api结果
     */
    @ApiOperation(value = "修改证件信息")
    @RequestMapping(value =  "/api/Document/{id}",method = {RequestMethod.PUT})
    public ApiResult<Document> Edit(@PathVariable Long id,@RequestBody Document model){
        ApiResult<Document> result=new ApiResult<Document>();
        try
        {
            model.setDocumentId(id);
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加证件信息
     * @param model 证件信息
     * @return api结果
     */
    @ApiOperation(value = "添加证件信息")
    @RequestMapping(value =  "/api/Document/",method = {RequestMethod.POST})
    public ApiResult<Document> Add(@RequestBody Document model){
         ApiResult<Document> result=new ApiResult<Document>();
        try
        {
            model.setCreateDate(new Date());
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    


    /**
     * 删除证件信息
     * @param id 证件ID
     * @return api结果
     */
    @ApiOperation(value = "根据类型删除证件头像信息")
    @RequestMapping(value =  "/api/Document/Avatar/{documentTypeId}",method = {RequestMethod.DELETE})
    public ApiResult<Object> DeleteAvatar(@PathVariable  Long documentTypeId){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.updateByDocumentTypeId(documentTypeId);

        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 删除证件信息
     * @param id 证件ID
     * @return api结果
     */
    @ApiOperation(value = "删除证件信息")
    @RequestMapping(value =  "/api/Document/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);

        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

